#include "Track.h"

Track::Track() {}

Track::Track(const QString &path) {
    _path = QUrl::fromLocalFile(path);
}

Track::Track(const QUrl &path) {
    _path =path;
}

// Track::Track(const QString &filePath) {
//     _path = QUrl::fromLocalFile(filePath);
//     TagLib::FileRef file(filePath.toStdString().c_str());
//     if (!file.isNull() && file.tag()) {
//         TagLib::Tag *tag = file.tag();

//         _title = QString::fromStdString(tag->title().to8Bit(true));
//         _artist = QString::fromStdString(tag->artist().to8Bit(true));
//         _album = QString::fromStdString(tag->album().to8Bit(true));
//         int year = tag->year();
//         _duration = file.audioProperties()->lengthInSeconds();

//         qDebug() << "标题:" << _title;
//         qDebug() << "艺术家:" << _artist;
//         qDebug() << "专辑:" << _album;
//         qDebug() << "年份:" << year;
//         qDebug() << "时长:" << _duration << "秒";
//     }
// }

void Track::setMetaData(const QMediaMetaData& metaData) {
    if (_hasMetaData) {
        qInfo()<<fileName()<<"has metadata, SKIP this track.";
        return;
    }
    else _hasMetaData = true;
    qInfo()<<fileName()<<"has NO metadata, setMetaData for this track.";
    _title = metaData.stringValue(QMediaMetaData::Title);
    _artist = metaData.value(QMediaMetaData::ContributingArtist).toStringList();
    _albumTitle = metaData.stringValue(QMediaMetaData::AlbumTitle);
    _albumArtist = metaData.stringValue(QMediaMetaData::AlbumArtist);
    _duration = metaData.value(QMediaMetaData::Duration).toLongLong();
}

QUrl Track::path() const { return _path; }

QString Track::fileName() const { return _path.fileName(); }

QString Track::title() const { return _title; }

QStringList Track::artist() const { return _artist; }

QString Track::albumTitle() const { return _albumTitle; }

QString Track::albumArtist() const { return _albumArtist; }

qint64 Track::duration() const { return _duration; }

bool Track::hasMetaData() const { return _hasMetaData; }
